<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="600px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="按钮名称" prop="name" style="width: 100%">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图标" prop="tb">
            <el-popover
              placement="bottom-start"
              width="450"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <custom-icon ref="iconSelect" @selected="selectedIcon" />
              <el-input slot="reference" v-model="form.tb" style="width: 450px;" placeholder="点击选择图标" readonly>
                <i
                  v-if="form.tb"
                  slot="prefix"
                  :class="form.tb"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import CustomIcon from '@/components/CustomIcon';
export default {
  name: 'FootMenuSet',
  components: { CustomIcon },
  data() {
    return {
      loading: false,
      dialogFormVisible: false,
      title: '新增栏目',
      form: {
        name: '',
        tb: ''
      },
      rules: {
        name: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
        tb: [{ required: true, message: '图标不能为空', trigger: 'change' }]
      }
    };
  },
  methods: {
    openEntity(data) {
      if (data !== null && undefined !== data) {
        this.form = data;
      } else {
        this.form.name = '';
        this.form.tb = '';
      }
      this.dialogFormVisible = true;
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('fetch-data', this.form);
          this.close();
        }
      });
    },
    selectedIcon(data) {
      this.form.tb = data;
    },
    close() {
      this.loading = false;
      this.dialogFormVisible = false;
    }
  }
};
</script>

<style scoped>

</style>
